package controladores;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.DefaultListModel;

import negocio.Medico;
import negocio.Sistema;
import ventanas.VentanaABMMedico;
import ventanas.VentanaBusquedaMedicos;

public class ControladorBusquedaMedicos implements ActionListener, MouseListener{

	private VentanaBusquedaMedicos v;
	private Sistema s;
	
	public ControladorBusquedaMedicos(VentanaBusquedaMedicos ventana){
		this.v = ventana;
		this.s = Sistema.obtenerInstancia();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==v.getbtnBuscarMedico()){
			List<Medico> Medicos = s.buscarMedicos(v.gettxtBuscarMedico().getText());
			DefaultListModel<Medico> modelo = new DefaultListModel<Medico>();
			
			for(Medico m:Medicos){
				modelo.addElement(m);
			}
			v.getLista().setModel(modelo);
			v.getLista().revalidate();
		}

		if(e.getSource()==v.getBtnC()){
			v.gettxtBuscarMedico().setText("");
			v.getLista().setModel(new DefaultListModel<Medico>());
		}
		
		if(e.getSource()==v.getbtnNuevoMedico()){
			new VentanaABMMedico(null);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(v.getLista().getModel().getSize()>0){
			if(e.getClickCount()==2){
				Medico m = v.getLista().getSelectedValue();
				new VentanaABMMedico(m);
			}
		}
	}


	@Override
	public void mousePressed(MouseEvent e) {}

	@Override
	public void mouseReleased(MouseEvent e) {}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

}
